package ps.guan.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



/**
 * 
 * @author is_zhoufeng
 *
 */
@Configuration
public class InitContextServlet extends HttpServlet {

	private static final Logger log = LoggerFactory.getLogger(InitContextServlet.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -3189110752397318951L;

	@Override
	public void init() throws ServletException {
		super.init();
		WebApplicationContext applicationContext = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		PropertiesConfigure propertyConfigure = applicationContext.getBean(PropertiesConfigure.class);
		if (propertyConfigure == null) {
			log.error("初始化ContextPath、StaticServer失败！，没找到" + PropertiesConfigure.class.getName() + "对象");
		}
		String cssserver = propertyConfigure.getProperties("app.cssserver.path");// css
		String jsserver = propertyConfigure.getProperties("app.jsserver.path");// js
		String imagesserver = propertyConfigure.getProperties("app.imagesserver.path");// imgage
		String userfileserver = propertyConfigure.getProperties("app.userfileserver.path");// 用户上传的显示路径
		String userfilepath = propertyConfigure.getProperties("app.userfilepath.dir");// 用户上传服务端的路径
		String httpbeginurl = propertyConfigure.getProperties("app.httpbeginurl.path");// 域名开始链接
		String videoserver = propertyConfigure.getProperties("app.video.path");// 域名开始链接
		if (StringUtils.isBlank(cssserver)) {
			log.error("没找到app.cssserver.path属性");
		}
		if (StringUtils.isBlank(jsserver)) {
			log.error("没找到app.jsserver.path属性");
		}
		if (StringUtils.isBlank(imagesserver)) {
			log.error("没找到app.imagesserver.path属性");
		}
		if (StringUtils.isBlank(userfileserver)) {
			log.error("没找到app.userfileserver.path属性");
		}
		if (StringUtils.isBlank(userfilepath)) {
			log.error("没找到app.userfilepath.path属性");
		}
		if (StringUtils.isBlank(httpbeginurl)) {
			log.error("没找到app.httpbeginurl.path属性");
		}
		if (StringUtils.isBlank(videoserver)) {
			log.error("没找到app.video.path属性");
		}
		
		getServletContext().setAttribute("cssserver", cssserver);
		getServletContext().setAttribute("jsserver", jsserver);
		getServletContext().setAttribute("imagesserver", imagesserver);
		getServletContext().setAttribute("userfileserver", userfileserver);
		getServletContext().setAttribute("userfilepath", userfilepath);
		getServletContext().setAttribute("httpbeginurl", httpbeginurl);
		getServletContext().setAttribute("videoserver", videoserver);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		throw new RuntimeException("不能访问该地址！");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		throw new RuntimeException("不能访问该地址！");
	}

}
